<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="profileObj.photo"
            @click="imageClickFn"
          />
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell
        title="名称"
        is-link
        :value="profileObj.name"
        @click="nameClickFn"
      />
      <van-cell
        title="生日"
        is-link
        :value="profileObj.birthday"
        @click="birthdayClickFn"
      />
    </van-cell-group>
    <van-dialog
      v-model="show"
      title="标题"
      show-cancel-button
      :before-close="beforeCloseFn"
    >
      <input type="text" v-fofo v-model="userName" />
    </van-dialog>
    <van-popup
      v-model="dateTimePickerShow"
      position="bottom"
      :style="{ height: '50%' }"
    >
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="dateTimePickerShow = false"
        @confirm="dateConfirmFn"
      />
    </van-popup>
  </div>
</template>

<script>
import {
  userProfileAPI,
  updateUserPhotoAPI,
  updateUserProfileAPI
} from '@/api'
// import { Notify } from 'vant'
import Notify from '@/ui/Notify.js'
import { dateFormat } from '@/utils/date'
import { mapMutations } from 'vuex'
export default {
  name: 'UserEdit',
  data () {
    return {
      profileObj: {},
      show: false,
      userName: '',
      minDate: new Date(1920, 0, 1), // 最下事件
      maxDate: new Date(), // 最大时间
      currentDate: new Date(), // 默认时间
      dateTimePickerShow: false
    }
  },
  async created () {
    const res = await userProfileAPI()
    this.profileObj = res.data.data
  },
  methods: {
    ...mapMutations(['SET_USERPHOTO']),
    imageClickFn () {
      this.$refs.iptFile.click()
    },
    async onFileChange (e) {
      if (e.target.files.length === 0) return
      // e.target.files[0] 这个就是文件对象
      const fd = new FormData()
      fd.append('photo', e.target.files[0])
      const res = await updateUserPhotoAPI(fd)
      this.profileObj.photo = res.data.data.photo
      this.SET_USERPHOTO(res.data.data.photo)
    },
    nameClickFn () {
      this.show = true
      this.userName = this.profileObj.name
    },
    async beforeCloseFn (action, done) {
      if (action === 'confirm') {
        const reg = /^[a-zA-z1-9\u4e00-\u9fa5]{1,7}$/
        if (reg.test(this.userName)) {
          done()
          await updateUserProfileAPI({
            name: this.userName
          })
          this.profileObj.name = this.userName
        } else {
          Notify({ type: 'warning', message: '用户名为1-7位中英文数字组合' })
          done(false)
        }
      } else {
        done()
      }
    },
    birthdayClickFn () {
      this.dateTimePickerShow = true
      this.currentDate = new Date(this.profileObj.birthday)
    },
    async dateConfirmFn (value) {
      await updateUserProfileAPI({
        birthday: dateFormat(value)
      })
      this.dateTimePickerShow = false
      this.profileObj.birthday = dateFormat(value)
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
/deep/ .van-dialog__content {
  text-align: center;
  input {
    border: none;
    outline: none;
    text-align: center;
  }
}
</style>
